/*************************************************
 *FileName:      membercenter.js
 *Description:   会员中心.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token
    var omony=0 ;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //数据加载
//      lode();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(lode)
        $('.content-title img').attr('src',DOMAIN_NAME+localStorage.replace_face);
        $('.content-title .title-right h3').text(localStorage.replace_nickname);
        
        $('.membercenter .main-ul li').on('tap',function(){//选择vip套餐
            $(this).addClass('pitch').siblings().removeClass('pitch');
            var oSpan=$(this).find('.main-right span:last-child').text();
            oSpan=oSpan-0;
            $('footer .price span').text(oSpan);
            omony=oSpan;
        })
        $('.btn').on('tap',function(){
            var oindex = $('.pitch').index();
            var type = oindex==0 ? 'monthly_vip' : oindex==1 ? 'more_month_vip' :oindex==2 ? 'quarter_vip' :'year_vip';
            var otext = $('.pitch h3').text();
            console.log(omony)
            mui.confirm('确定支付'+omony+'换币购买<br />'+otext+'？','提示',['确定','取消'],function(e){
                if(e.index==0){
                    oppervip(type);
                }
            },'div')
            
        })
    }
    
    /**
     * lode
     * 数据加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function lode(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/vipCenter",
            dataType:'jsonp',
            data: { uid: uid, token: token,},
            success: function(data) {
                if(data.error_code == 0){
                    var timestamp = Date.parse(new Date());
                    var otime =timesub(data.data.vip_time);
                    $('.ovmoney .color').text(data.data.balance)
                    if(data.data.vip_time*1000>timestamp){
                        $('.content-title .title-right p').html('你的会员<span>'+otime+'</span>到期')
                    }
                    $('.main-ul li').eq(0).find('.main-right span').text(data.data.monthly_vip);
                    $('.main-ul li').eq(1).find('.main-right span').text(data.data.more_month_vip);
                    $('.main-ul li').eq(2).find('.main-right span').text(data.data.quarter_vip);
                    $('.main-ul li').eq(3).find('.main-right span').text(data.data.year_vip);
                    
                    $('footer .price span').text(data.data.monthly_vip);
                    omony=parseInt(data.data.monthly_vip);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        });         
    }

//时间计算
    function timesub(time){
        var date = new Date(time * 1000);
        var year = date.getFullYear();
        var mou = date.getMonth() + 1;
        var day = date.getDate();
        mou<10 ? mou = '0'+mou:true;
        day<10 ? day = '0'+day:true;
        var newtime = year + '-' + mou + '-' + day  ;
        return newtime;
    }
    /**
     * oppervip
     * 开通
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function oppervip(type){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/openVip",
            dataType:'jsonp',
            data: { uid: uid, token: token,type:type},
            success: function(data) {
                if(data.error_code == 0){
                    mui.toast('开通成功',{ duration:2000, type:'div' });
                    setTimeout(function(){mui.back()},500);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        });         
    }